function arrayAvgPlots(handles)
% arrayAvgPlots
%
%Editing:
% gs april 2005 privatized for mapAnalysis2p0
% -------------------------------------------------------------------

% set up the figure
x = .2; y = .05; w = .7; h = .7; 
figure('Units', 'normalized', 'Position', [x y w h], 'Name', 'arrayAvgPlots', ...
    'NumberTitle', 'off', 'Color', 'w');
subplotRows = 4; subplotCols = 3; plotnum = 0;

% map images - general settings
[sizeX sizeY z] = size(handles.data.map.mapActive.mapDataArray(:,:,1));
magFacX = (handles.data.map.mapActive.mapSpacing * (sizeX-1))/2;
magFacY = (handles.data.map.mapActive.mapSpacing * (sizeY-1))/2;
plotNameTxtPos = [0 1.05];
colorBarTxtPos = [1.13 -.08];

% polarity stuff -- gs 2006 12 02
if strcmp(handles.data.analysis.eventPolaritySyn, 'down')
    plotPolarityFactor = 1;
elseif strcmp(handles.data.analysis.eventPolaritySyn, 'up')
    plotPolarityFactor = -1;
end

% SYNAPTIC WINDOW PLOTS *****************************************
% genMin (or genMax) map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
if strcmp(handles.data.analysis.eventPolaritySyn, 'down')
    map = handles.data.map.mapStack.mapAvgMin;
else
    map = handles.data.map.mapStack.mapAvgMax;
end
map(isnan(map)) = plotPolarityFactor * inf;
result = map;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% lowerLim = min(min(result));
lowerLim = min(min(result(result>-inf)));
upperLim = max(max(result(result<inf)));
upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
set(gca, 'CLim', [lowerLim upperLimTweaked]);
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
ylabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Peak', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
try
    colorbar('vert');
    text(colorBarTxtPos(1), colorBarTxtPos(2),'pA', 'Units', 'Normalized');
catch
    disp('arrayAvgPlots: unable to create colorbar for image')
end

% genMean map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
map = handles.data.map.mapStack.mapAvgMean;
map(isnan(map)) = plotPolarityFactor * inf;
result = map;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% lowerLim = min(min(result));
lowerLim = min(min(result(result>-inf)));
upperLim = max(max(result(result<inf)));
upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
set(gca, 'CLim', [lowerLim upperLimTweaked]);
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Mean', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
try
    colorbar('vert');
    text(colorBarTxtPos(1), colorBarTxtPos(2),'pA', 'Units', 'Normalized');
catch
    disp('arrayAvgPlots: unable to create colorbar for image')
end

% genOnset map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
if strcmp(handles.data.analysis.eventPolaritySyn, 'down')
    map = handles.data.map.mapStack.mapAvgOnset;
else
    map = handles.data.map.mapStack.mapAvgMaxOnset;
end
map(isnan(map)) = plotPolarityFactor * inf;
map(map==0) = plotPolarityFactor * inf;
result = map;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% lowerLim = min(min(result));
lowerLim = min(min(result(result>-inf)));
upperLim = max(max(result(result<inf)));
upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
set(gca, 'CLim', [lowerLim upperLimTweaked]);
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Onset', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
try
    colorbar('vert');
    text(colorBarTxtPos(1), colorBarTxtPos(2), '(sec)', 'Units', 'Normalized');
catch
    disp('arrayAvgPlots: unable to create colorbar for image')
end

% DIRECT WINDOW PLOTS *****************************************
% genMin map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
map = handles.data.map.mapStack.mapAvgMinED;
map(isnan(map)) = plotPolarityFactor * inf;;
result = map;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% lowerLim = min(min(result));
lowerLim = min(min(result(result>-inf)));
upperLim = max(max(result(result<inf)));
%     if sum(sum(result~=inf)) ~= 0
if lowerLim ~= upperLim & ~isempty(upperLim)
    upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
    set(gca, 'CLim', [lowerLim upperLimTweaked]);
else
    set(gca, 'CLim', [0 inf]);
end
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
ylabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Peak', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
try
    colorbar('vert');
    text(colorBarTxtPos(1), colorBarTxtPos(2),'pA', 'Units', 'Normalized');
catch
    disp('arrayAvgPlots: unable to create colorbar for image')
end

% genMean map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
map = handles.data.map.mapStack.mapAvgMeanED;
map(isnan(map)) = plotPolarityFactor * inf;;
result = map;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% lowerLim = min(min(result));
lowerLim = min(min(result(result>-inf)));
upperLim = max(max(result(result<inf)));
if lowerLim ~= upperLim & ~isempty(upperLim)
    upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
    set(gca, 'CLim', [lowerLim upperLimTweaked]);
else
    set(gca, 'CLim', [0 inf]);
end
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Mean', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
try
    colorbar('vert');
    text(colorBarTxtPos(1), colorBarTxtPos(2),'pA', 'Units', 'Normalized');
catch
    disp('arrayAvgPlots: unable to create colorbar for image')
end

% genOnset map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
map = handles.data.map.mapStack.mapAvgOnsetED;
map(isnan(map)) = plotPolarityFactor * inf;;
map(map==0) = plotPolarityFactor * inf;;
result = map;
imagesc(result, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
% lowerLim = min(min(result));
lowerLim = min(min(result(result>-inf)));
upperLim = max(max(result(result<inf)));
if lowerLim ~= upperLim & ~isempty(upperLim)
    upperLimTweaked = upperLim + 0.02*(abs(upperLim-lowerLim));
    set(gca, 'CLim', [lowerLim upperLimTweaked]);
else
    set(gca, 'CLim', [0 inf]);
end
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Onset', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
try
    colorbar('vert');
    text(colorBarTxtPos(1), colorBarTxtPos(2), '(sec)', 'Units', 'Normalized');
catch
	disp('arrayAvgPlots: unable to create colorbar for image')
end

plotnum = plotnum+1;


% summed binary/thresholded map -- shows how many responses each site gave
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
[r,c,p] = size(handles.data.map.mapStack.mapStackHistoThresh);
s=-sum(handles.data.map.mapStack.mapStackHistoThresh, 3);
s(isnan(s))=inf;
imagesc(s, 'XData', [-magFacX magFacX], 'YData', [-magFacY magFacY]);
set(gca, 'CLim', [-p .1]);
colorbar;
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
xlabel('micrometers');
text(plotNameTxtPos(1), plotNameTxtPos(2),'Events per site', ...
    'Units', 'Normalized', 'FontWeight', 'Bold');
try
    colorbar('vert');
    text(colorBarTxtPos(1), colorBarTxtPos(2), '#', 'Units', 'Normalized');
catch
	disp('arrayAvgPlots: unable to create colorbar for image')
end

% ONSET LATENCY HISTOGRAM

% data for histogram -- sum of individual maps' data
sumData = sum(handles.data.map.mapStack.mapStackLatHistoN, 2);

% histo params
leftLim = 0;
binWidth = .001;
rightLim = handles.data.analysis.synapticWindow;
histoEdges = (leftLim : binWidth : rightLim);

try
    plotnum = plotnum+1;
    hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
    h = bar(handles.data.map.mapStack.mapStackLatHistoEdges, sumData, 'histc');
    set(h, 'FaceColor', 'r', 'LineStyle', 'none');
    set(gca, 'XLim', [leftLim rightLim]);
    xlabel('Onset latency (sec)');
    ylabel('Number');
    edEnd = handles.data.analysis.directWindow;
    hline = line([edEnd edEnd], [min(get(gca,'YLim')) max(get(gca,'YLim'))], ...
        'Color', 'b', 'LineStyle', ':');
catch
     error(lasterror);
end


colormap(jet2);
if ~handles.data.analysis.currentClamp & strcmp(handles.data.analysis.eventPolaritySyn, 'down')
    colormap(flipud(colormap(jet2)));
end



% image of slice (in mapping position) -------------------------------------
% handles.data.image.imgDir = imgDir;
% handles.data.image.imgName = name;
% handles.data.image.img = R;

plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);

handles.data.analysis.hSliceImg = imagesc(handles.data.image.img);
daspect([1 1 1]);

% set(himg, 'ButtonDownFcn', 'addSliceImage');
set(gca, 'ButtonDownFcn', 'addSliceImage3p0');
% title('Click to upload an image');
title(handles.data.image.imgName);

% distance scaling
try
    xrange = handles.data.acq.imageXrange;
    yrange = handles.data.acq.imageYrange;
catch
    xrange = 1900; % default param's from Ingrid's rig
    yrange = 1520;
    disp('arrayAvgPlots: using defaults for xrange and yrange that may be invalid. Check.');
end
set(handles.data.analysis.hSliceImg, ...
    'XData', [-xrange/2 xrange/2], 'YData', [-yrange/2 yrange/2]);
axis tight;

% handleList = [handles.data.analysis.haxSliceImg_currentTrace; ...
%     handles.data.analysis.hRectMapArea; handles.data.analysis.hBlankMap; ...
%     handles.data.analysis.hSliceImg];
% set(data.handles.haxSliceImg, 'Children', handleList); 

% % blank map array
% hold on;
% himgBlank = imagesc(zeros(16,16));
% set(himgBlank, 'AlphaData', .1, 'XData', [-data.map.magFacX data.map.magFacX], ...
%     'YData', [-data.map.magFacY data.map.magFacY]);
% set(data.handles.haxSliceImg, 'Position', [0.4919    0.7054    0.2840    0.2555], ...
%     'ButtonDownFcn', 'addSliceImage');
% daspect([1 1 1]);
% axis tight;
% pixTrace(himgBlank);

% % rectangle to mark mapping region
% hrect = rectangle;
% xdata = get(data.handles.himgP1, 'XData');
% ydata = get(data.handles.himgP1, 'YData');
% set(hrect, 'Position', [xdata(1) ydata(1) xdata(2)-xdata(1) ydata(2)-ydata(1)], ...
%     'EdgeColor', 'y', 'LineStyle', ':');

set(gcf, 'UserData', handles);
% -----------------------------------------------------------------------

if handles.chkWithRsRm
    plotnum = plotnum+1;
    hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
    plotCellParameters(handles, hsub(plotnum));
end

% set(hsub(4:end), 'PlotBoxAspectRatio', [1 1 1]);
% get(gcf, 'Position');

% titleStr = [handles.data.map.mapActive.uncagingPathName(end-11:end-7) ', average maps'];
% if strcmp(handles.data.map.mapActive.uncagingPathName(end-11:end-7), 'races')
%     titleStr = [handles.data.map.mapActive.uncagingPathName(end-18:end-14) ', average maps'];
% end

titleStr = [handles.data.analysis.experimentName ', average maps'];

text('String', titleStr, 'Units', 'Normalized', 'Position', [0 1.2], ...
    'FontWeight', 'Bold', 'Parent', hsub(1), 'Interpreter', 'none');

text('String', 'Synaptic', 'Units', 'Normalized', 'Position', [-.5 0.3], ...
    'FontWeight', 'Bold', 'Parent', hsub(1), 'Rotation', 90);

text('String', 'Direct', 'Units', 'Normalized', 'Position', [-.5 0.35], ...
    'FontWeight', 'Bold', 'Parent', hsub(4), 'Rotation', 90);

% text('String', 'All', 'Units', 'Normalized', 'Position', [-.5 0.4], ...
%     'FontWeight', 'Bold', 'Parent', hsub(7), 'Rotation', 90);


